package com.oocl.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerService;
import com.oocl.service.impl.CustomerServiceImpl;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		    request.setCharacterEncoding("utf-8");
			Customer c = getCustomer(request, response);
			
			CustomerService cSer = new CustomerServiceImpl();
			boolean result = cSer.addCustomer(c);
			
			if(result){
				request.getRequestDispatcher("succeed").forward(request, response);;
			}else{
				
			}
			
		
		
	}

	private Customer getCustomer(HttpServletRequest request, HttpServletResponse response) {
		DiskFileItemFactory factory = new DiskFileItemFactory(20480,new File("D:/temp"));
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setFileSizeMax(1024000);
		Customer c = new Customer();
		try {
		List<FileItem> list = upload.parseRequest(request);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		for(FileItem  item :list){
			if(item.isFormField()){
				System.out.println(item.getFieldName()+":"+item.getString());
				if("username".equals(item.getFieldName())){
					if(item.getString()==null&&"".equals(item.getString())){
						response.sendRedirect("errror?errorType=1");
					}else{
						c.setUname(item.getString());
					}
				}else if("salary".equals(item.getFieldName())){
					c.setSalary(Double.parseDouble(item.getString()));
				}else if("birth".equals(item.getFieldName())){
					if(item.getString()==null&&"".equals(item.getString())){
						response.sendRedirect("errror?errorType=4");
					}
					try {
						c.setBirth(df.parse(item.getString()));
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}else if("sex".equals(item.getFieldName())){
					c.setSex(Boolean.parseBoolean(item.getString()));
				}
				
			}else{
				InputStream in = item.getInputStream();
				byte[] bs = new byte[in.available()];
			    in.read(bs);
				c.setPortrait(bs);
			}
		}
	    }catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NumberFormatException e) {
			try {
				response.sendRedirect("errror?errorType=2");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	    }catch (FileUploadException e) {
	    	try {
				response.sendRedirect("errror?errorType=3");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	  }
		return c;
	}
	
	
	

}
